package com.faithnmn.learnenglishbc.db;


import com.faithnmn.learnenglishbc.model.Quote;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;


public class TblQuote {
	public static final String TABLE_NAME = "tbl_fb_learn_en";

	public class Column {
		public static final String ID = "_id";
		public static final String CREATED_TIME = "created_time";
		public static final String MESSAGE = "message";
	}
	
	public static void createTable(SQLiteDatabase db) {

		String CREATE_QUOTE_TABLE = "CREATE TABLE " + TABLE_NAME + "(" + Column.ID
				+ " TEXT PRIMARY KEY ," + Column.CREATED_TIME
				+ " TEXT," + Column.MESSAGE + " TEXT)";
		db.execSQL(CREATE_QUOTE_TABLE);
	}
//			byte[] mBlob = cursor.getBlob(cursor.getColumnIndex(Column.NAME));
//			if (mBlob != null) {
//				String content = new String(mBlob);
	//update all db
	
	public static Quote getQuote(SQLiteDatabase db) {
		Quote quo=null;
		String id;
		Cursor c = db.query(TABLE_NAME, new String[] {Column.ID, Column.CREATED_TIME,Column.MESSAGE}, null, null, null,null, " created_time desc ", "1");
		if (c.getCount() > 0) {
			c.moveToFirst();
			id=c.getString(c.getColumnIndex(Column.ID));
			quo = new Quote(id, c.getString(c.getColumnIndex(Column.CREATED_TIME)),c.getString(c.getColumnIndex(Column.MESSAGE)));
		}else{
			Log.e("DB","out of quote!");
			c = db.query(TABLE_NAME, new String[] {Column.ID, Column.CREATED_TIME,Column.MESSAGE},null, null, null,null, "RANDOM()", "1");
		}
		c.close();
		db.close();
		return quo;
	}
	
}
